// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database()
  let dd =new Date()
  const _ =db.command
  
  let recoupons =[]
  let  couponsMarket =  db.collection('couponsMarket').where({ status:1, deadline : _.gt(dd),startTime:_.lt(dd) }).get();
 // let  usercoupons =  db.collection('coupons').where({_openid:wxContext.OPENID }).get();
  let  couponsMarketdata =  (await couponsMarket).data
  //let usercoupondata = (await usercoupons).data
  for (let index = 0; index < couponsMarketdata.length; index++) {
    const couponmarketdata = couponsMarketdata[index];
    console.log(couponmarketdata)
    let  userc = db.collection('coupons').where({_openid:wxContext.OPENID ,couponsMarketid:couponmarketdata._id}).get();
    console.log(JSON.stringify((await userc).data))
    if(couponmarketdata.limitedreceive >(await userc).data.length){
      couponmarketdata['lingquStatus']=true
    }else{
      couponmarketdata['lingquStatus']=false
    }
    
      
    
    
  }
   
  return couponsMarket
}